<?php
/**
 * Latest forum posts
 *
 * @uses $vars['entity']
 */

if ($vars['entity']->forum_enable == 'no') {
	return true;
}

$group = $vars['entity'];


$all_link = elgg_view('output/url', array(
	'href' => "discussion/owner/$group->guid",
	'text' => elgg_echo('link:view:all'),
	'is_trusted' => true,
));

elgg_push_context('widgets');
$options = array(
	'type' => 'object',
	'subtype' => 'groupforumtopic',
	'container_guid' => $group->getGUID(),
	'limit' => 6,
	'full_view' => false,
	'pagination' => false,
);
$content = elgg_list_entities($options);
elgg_pop_context();

if (!$content) {
	$content = '<p>' . elgg_echo('discussion:none') . '</p>';
}

$new_link = elgg_view('output/url', array(
	'href' => "discussion/add/" . $group->getGUID(),
	'text' => elgg_echo('groups:addtopic'),
	'is_trusted' => true,
));

elgg_load_library('elgg:discussion');

$body_vars = discussion_prepare_form_vars();
$content .= elgg_view_form('discussion/save', array(), $body_vars);

echo elgg_view('groups/profile/module', array(
	'title' => elgg_echo('discussion:group'),
	'content' => $content,
	'all_link' => $all_link,
	//'add_link' => $new_link,
));
?>
<script>
$(".elgg-form-discussion-save").submit(function(){
	var form = $(this);
	form.append("<input type='hidden' name='ajax_flag' value='1'>");
	var data = form.serialize();
	//console.log(form.attr('action'));
	//console.log(data);
	$.ajax({
		type: form.attr('method'),
		url: form.attr('action'),
		data: data,
		success: function(date){
			//console.log(data);
			var ul = form.prev('ul.elgg-list-entity');
			//console.dir(ul);
			if (ul.length > 0) {
				ul.remove();
			}
			form.parent().prepend(date);
			form.find('.elgg-input-text').val('');
			form.find('textarea').val('');
		}
	})
	return false;
});
</script>